#include <stdio.h>
#include <string.h>

int getparameter(char *key, char *value) {
    FILE *file = fopen("config.ini", "r");
    if (!file) {
        strcpy(value, "无法打开文件");
        return -1;
    }

    while (1) {
        char line[100];
        if (!fgets(line, sizeof(line), file)) {
            strcpy(value, "没有要找的内容");
            fclose(file);
            return -1;
        }

        // 去除换行符
        line[strcspn(line, "\n")] = '\0';

        char *line_key = strtok(line, "=");
        if (!line_key) continue; // 跳过无效行

        if (strcmp(key, line_key) == 0) {
            char *line_value = strtok(NULL, "=");
            if (!line_value) {
                strcpy(value, "值缺失");
                fclose(file);
                return -1;
            }
            strcpy(value, line_value);
            fclose(file);
            return 0;
        }
    }
}

int main(int argc, char *argv[]) {
    char ip[100];
    bzero(ip, sizeof(ip));
    getparameter("ip", ip);
    printf("ip is = %s\n", ip);

    char port[100];
    bzero(port, sizeof(port));
    getparameter("port", port);
    printf("port is = %s\n", port);

    return 0;
}